﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xinkui.WebModel.Cache.View;
using Xinkui.Common;
using Xinkui.DataAccess.Common;

namespace Xinkui.WebModel.Cache {
    public class DictCache {

        public static DictView Get(int type, int idx) {
            var dict = Dict(type);
            DictView view = null;
            if (dict != null) {
                dict.TryGetValue(idx, out view);
            }
            return view;
        }

        public static string GetName(int type, int idx) {
            var dv = Get(type, idx);
            if (dv != null) return dv.Value;
            else return string.Empty;
        }

        public static Dictionary<int, DictView> Dict(int type) {
            return CacheProxy.Get<Dictionary<int, DictView>>(SystemConst.CacheConst.GetCommonDictKey(type), () =>
            {
                return DictFromDb(type);
            });
        }

        public static Dictionary<int, DictView> DictFromDb(int type) {


            return Singleton<CommonDictDataAccess>.Instance.CommonDict_ListByCode(type).ToDictionary(u => u.Id, u => new DictView {
                DictType = type,
                Idx = u.Id,
                Value = u.Name
            });
            //  return new Dictionary<int, DictView>(0);
        }
    }
}
